/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.test;


import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.isfd.domain.IsfdDatabaseModelConfig;
import com.comac.ins.isfd.domain.bo.IsfdDatabaseDataImportRecordBo;
import com.comac.ins.isfd.domain.vo.IsfdDatabaseDataImportRecordVo;
import com.comac.ins.isfd.domain.vo.IsfdDatabaseModelConfigTableVo;
import com.comac.ins.isfd.domain.vo.IsfdDatabaseModelConfigVo;
import com.comac.ins.isfd.mapper.IsfdDatabaseModelConfigMapper;
import com.comac.ins.isfd.service.IIsfdDatabaseDataImportRecordService;
import com.comac.ins.isfd.service.IIsfdDatabaseModelConfigService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@Slf4j
@SpringBootTest
public class IsfdImportRecordCheckTest {

    @Autowired
    private IIsfdDatabaseDataImportRecordService importRecordService;

    @Autowired
    private IIsfdDatabaseModelConfigService configService;

    @Autowired
    private IsfdDatabaseModelConfigMapper configMapper;

    @Test
    public void test1() {

        IsfdDatabaseDataImportRecordBo recordBo = new IsfdDatabaseDataImportRecordBo();
        List<IsfdDatabaseDataImportRecordVo> recordVos = importRecordService.queryList(recordBo);
        for (IsfdDatabaseDataImportRecordVo recordVo : recordVos) {
            Long modelId = recordVo.getModelId();
            String tableNames = recordVo.getTableName();
            if (modelId == null && StringUtils.isBlank(tableNames)) {
                continue;
            }
            String tableName = null;
            IsfdDatabaseModelConfig topConfig = null;
            if (modelId == null) {
                tableName = tableNames.split(",")[0];
                if (StringUtils.isBlank(tableName)) {
                    continue;
                }
                IsfdDatabaseModelConfigVo configVo = configMapper.selectByTableName(tableName);
                topConfig = configService.queryByIdReturnTopParent(configVo.getId());

            } else {
                topConfig = configService.queryByIdReturnTopParent(modelId);
            }

            if (topConfig == null) {
                continue;
            }

            List<IsfdDatabaseModelConfigTableVo> configVos = configService.getFieldTableList(topConfig.getId());
            List<String> allTables = configVos.stream()
                            .map(IsfdDatabaseModelConfigTableVo::getTableName)
                                    .toList();
            for (String name : allTables){
                log.warn("recordId: " + recordVo.getId() + ", tableName: \n" + name);
                importRecordService.checkAndUpdateSubRecord(recordVo.getId(), name);
            }

        }

        System.out.println("success");
    }
}
